package com.example.ui.contact;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.example.entity.ShareEntity;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.ui.contact.adapter.PersonAdapter;
import com.example.ui.contact.bean.CommentItem;
import com.example.ui.contact.bean.ShareItem;
import com.example.ui.contact.chat.ChatActivity;
import com.example.ui.leftmenu.Item08View;
import com.example.util.BitmapLoad;
import com.example.util.Util;


public class PersonHomeActivity extends Activity{
	
//	private ImageView mPage0;
//	private ImageView mPage1;
//	private int currIndex = 0;
	
	PersonHomeActivity context;
	private Button leftBtn;
	private Button rightBtn;
	private TextView textTitle;
	private ListView listview;  //PullToRefreshListView
	public static List<Map<String, Object>> listData;
	private List<Map<String, Object>> galleryData;
	private List<ShareItem> msgList = new ArrayList<ShareItem>();// item对象数组
	// 头布局文件
	private View headView;	
	private ImageView wall0Img,headImg,msgImg;
	private Gallery wallGallery;
	private TextView wallNumText,personName,myTitle,myNeed,mySupply;
	
	public static Handler handler;
	public static final int SUCCESS = 0;
	public static final int FAIL = 1;
	
    private String[] mStrings = {
            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
            "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
            "Allgauer Emmentaler"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.personhome);
		
		findView();
		ListenerAction();
		listView();
		wallGallery();
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case SUCCESS:
					
					break;
				case FAIL:
					
					break;

				default:
					break;
				}
				super.handleMessage(msg);
			}
		};
		

	}
	
	public void findView(){
		leftBtn = (Button) findViewById(R.id.layout_title_left_btn);
		textTitle = (TextView)findViewById(R.id.layout_title_middle_text);
		rightBtn = (Button)findViewById(R.id.layout_title_right_btn);
		
		listview = (ListView)findViewById(R.id.person_introduce_listview);
		headView= LayoutInflater.from(this).inflate(R.layout.personhome_headview, null);
		
		wall0Img= (ImageView)headView.findViewById(R.id.personhome_headview_img_wall0_img);//默认的背景墙图片
		wallGallery=(Gallery)headView.findViewById(R.id.personhome_headview_img_wall);//背景墙
		headImg= (ImageView)headView.findViewById(R.id.personhome_headview_head_img);//头像
		msgImg = (ImageView)headView.findViewById(R.id.personhome_headview_msg_img);
		wallNumText=(TextView)headView.findViewById(R.id.personhome_headview_img_wall_num_text);//背景墙编号 --如： 1/5
		personName=(TextView)headView.findViewById(R.id.personhome_headview_name_text);//姓名
		myTitle=(TextView)headView.findViewById(R.id.personhome_headview_mytitle);//标题
		myNeed=(TextView)headView.findViewById(R.id.personhome_headview_need);//需求
		mySupply=(TextView)headView.findViewById(R.id.personhome_headview_supply);//供应
		
		init();

	}
	private void init() {
		String name = getIntent().getStringExtra("mName");
		String id = getIntent().getStringExtra("mId");
		rightBtn.setVisibility(View.INVISIBLE);
		textTitle.setText("个人主页");
		leftBtn.setText("返回");
		personName.setText(name);
		
	}
	public void ListenerAction(){
		
		leftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		msgImg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.toast(context, "此功能赞不支持");//
				//Intent intent = new Intent(context,ChatActivity.class);
				//startActivity(intent);
			}
		});
	}

	private void wallGallery() {
		galleryData=new ArrayList<Map<String,Object>>();
		
		wallGallery.setAdapter(wallGalleryAdapter);
		
		wallGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int currentPosition = arg2+1;
				if(galleryData.size()==0){
					wallNumText.setVisibility(View.GONE);
				}else{
					wallNumText.setText(currentPosition+"/"+galleryData.size());
				}
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	private void listView() {
//		listview.setOnRefreshListener(new OnRefreshListener() {
//	        @Override
//	        public void onRefresh() {
//	            new GetDataTask().execute();
//	        }
//	    });
		
		//getShare();
//		for(int i = 0;i<5;i++){
//			ShareEntity entity = new ShareEntity();
//			msgList.add(entity);
//		}
		msgList = Item08View.share_list;
		listview.addHeaderView(headView);
		PersonAdapter adpter = new PersonAdapter(this,msgList);//  listData
		listview.setAdapter(adpter);
		//Communication.getMyShare(context, "123", 0);//请求数据
		
	}	

    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                ;
            }
            return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            //mListItems.addFirst("Added after refresh...");
        	//ShareEntity entity = new ShareEntity();
        	//msgList.add(entity);
          //  listview.onRefreshComplete();

            super.onPostExecute(result);
        }
    }
    
	private BaseAdapter wallGalleryAdapter = new BaseAdapter() {
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = LayoutInflater.from(context).inflate(R.layout.homepage_gallery_img, null);
				holder.img = (ImageView)convertView.findViewById(R.id.homepage_gallery_img);
				convertView.setTag(holder);
			}else{
				holder=(Holder)convertView.getTag();
				
			}
			
			
			try {
				Bitmap bit = null;
				/*
				String imgUrl = (String)galleryData.get(position).get("photo").toString();
				if(imgUrl!=null){
					bit=BitmapLoad.loadBitmap(context,imgUrl);
					Drawable dra = new BitmapDrawable(bit);
					holder.img.setBackgroundDrawable(dra);
				}else{
					Util.toast(context, "网络连接有误，请重试");
					holder.img.setBackgroundResource(R.drawable.wall_bg);
				}
				*/
				holder.img.setBackgroundResource(R.drawable.wall_bg);//测试专用-----实际使用上面的方法获得galleryData里面的数据
				holder.img.setScaleType(ImageView.ScaleType.CENTER);
				holder.img.setAdjustViewBounds(true);
				holder.img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return convertView;  
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			return position;
		}
		
		@Override
		public int getCount() {
			if(galleryData.size()==0){
				wallGallery.setVisibility(View.GONE);
				return 0;
			}
			return galleryData.size();
		}
	};
	
	class Holder {
		ImageView img;
	}
	
	
	
}
